/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.baseConfig.controller;

import com.koron.workflow.applicationManag.bean.AdministratorAppQueryBean;
import com.koron.workflow.baseConfig.bean.DepartmentStaffBean;
import com.koron.workflow.baseConfig.bean.PostBean;
import com.koron.workflow.baseConfig.service.PostService;
import com.koron.workflow.common.annotation.CaseUser;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * @author qingfeng
 * @description: TODO
 * @date 2020/4/13 11:59
 */
@Api(value = "post", description = "岗位")
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService service;

    private static final Logger logger = LoggerFactory.getLogger(PostController.class);

    @ApiOperation("岗位分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "岗位名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("queryForList.htm")
    public MessageBean queryForList(@ApiIgnore String name, @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        if (page != null){
            page = page - 1;
            page = page * pageSize;
            if (page < 0){
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("页码不能小于1");
                return msg;
            }
        }
        try {
            PageBean<PostBean> pageBean = ADOConnection.runTask(service, "queryForList", PageBean.class,
                    name, page , pageSize);
            msg.setData(pageBean);
        } catch (Exception e) {
            logger.error("分页查询岗位失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("保存岗位")
    @PostMapping("/save.htm")
    public MessageBean save(@ApiIgnore @CaseUser StaffBean user,@RequestBody @Valid PostBean bean) {
        try {
            return ADOConnection.runTask(service,"save",MessageBean.class,bean,user);
        }catch (Exception e) {
            logger.error("保存岗位失败",e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(),BusinessCode.EXCEPTION.getDescription(),String.class);
        }
    }

    @ApiOperation("根据ID删除岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "岗位id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/delete.htm")
    public MessageBean delete(@ApiIgnore String id) {
        try {
            ADOConnection.runTask(service,"delete",void.class,id);
            return MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),String.class);
        }catch (Exception e) {
            logger.error("删除岗位失败",e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(),BusinessCode.EXCEPTION.getDescription(),String.class);
        }
    }

    @ApiOperation("查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/queryForObject.htm")
    public MessageBean queryForObject(@ApiIgnore String id) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),String.class);
        try {
            PostBean data = ADOConnection.runTask(service,"queryForObject",PostBean.class,id);
            msg.setData(data);
            return msg;
        }catch (Exception e) {
            logger.error("查询失败",e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(),BusinessCode.EXCEPTION.getDescription(),String.class);
        }
    }

    @ApiOperation("删除岗位用户关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/deletePostStaffRel.htm")
    public MessageBean deletePostStaffRel(@ApiIgnore String id) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),String.class);
        try {
            ADOConnection.runTask(service,"deletePostStaffRel",void.class,id);
            return msg;
        }catch (Exception e) {
            logger.error("删除失败",e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(),BusinessCode.EXCEPTION.getDescription(),String.class);
        }
    }

    @ApiOperation("查询岗位所属用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "postId", value = "岗位id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "departmentName", value = "部门名称", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "staffName", value = "用户名称", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("queryPostStaff.htm")
    public MessageBean queryPostStaff(@ApiIgnore String postId, @ApiIgnore String departmentName, @ApiIgnore String staffName) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), List.class);
        try {
            List<DepartmentStaffBean> data = ADOConnection.runTask(service, "queryPostStaff", List.class,
                    postId, departmentName , staffName);
            msg.setData(data);
        } catch (Exception e) {
            logger.error("查询失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("更新岗位用户关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "postId", value = "岗位id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffId", value = "用户id,分割", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/updatePostStaffRel.htm")
    public MessageBean updatePostStaffRel(@ApiIgnore String postId, @ApiIgnore String staffId) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),String.class);
        try {
            ADOConnection.runTask(service,"updatePostStaffRel",void.class,postId,staffId);
            return msg;
        }catch (Exception e) {
            logger.error("更新失败",e);
            return MessageBean.create(BusinessCode.EXCEPTION.getCode(),BusinessCode.EXCEPTION.getDescription(),String.class);
        }
    }

}
